package pl.edu.agh.neuraleconomy.ui.component;

import java.util.List;

import lombok.Getter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import pl.edu.agh.neuraleconomy.model.simulation.Simulation;
import pl.edu.agh.neuraleconomy.persistence.base.DaoProvider;

public class LoadSimulationDialog extends OkCancelDialog {
	private Combo simCombo;
	@Getter
	private Long simulationId;

	public LoadSimulationDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected void init(Composite parent) {
		parent.setLayout(new GridLayout(2, false));

		(new Label(parent, SWT.NONE)).setText("Simulation name");
		simCombo = new Combo(parent, SWT.READ_ONLY);
		simCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		populateCombo(simCombo);
	}

	private void populateCombo(Combo combo) {

		List<Simulation> simulations = DaoProvider.getSimulationDao().getAll();
		for (Simulation s : simulations) {
			String name = s.getName();
			combo.add(name);
			combo.setData(name, s.getId());
		}
		if (combo.getItemCount() > 0) {
			combo.setText(combo.getItem(0));
		}
	}

	private Long getSelectedId() {
		int indx = simCombo.getSelectionIndex();
		if (indx < 0) {
			return null;
		}
		String selectedCompanyName = simCombo.getItem(indx);
		return (Long) simCombo.getData(selectedCompanyName);
	}
	
	
	@Override
	protected void saveInput() {
		this.simulationId = getSelectedId();
	}

	@Override
	protected boolean validateInput() {
		return getSelectedId() != null;
	}

}
